﻿using MyWeb.Model.ScheduledTask;
using MyWeb.Model.User;
using MyWeb.Repository.Base;
using MyWeb.Services.Base;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace MyWeb.Services.ScheduledTask
{
    public class JobsModelService: IJobsModelService
    {
        private readonly IBaseRepository<JobsModel> BaseDal;
        public JobsModelService(IBaseRepository<JobsModel> baseDal)
        {
            this.BaseDal = baseDal;
        }
        /// <summary>
        /// 添加Job
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<bool> AddJob(JobsModel model)
        {
            model.Id = 0;

            var date = await BaseDal.Add(model);
            if (date > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 根据条件查询Job列表
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public async Task<List<JobsModel>> GetJobs(Expression<Func<JobsModel, bool>> where)
        {
            return await BaseDal.Db.Queryable<JobsModel>().Where(where).ToListAsync();
        }


        /// <summary>
        /// 删除数据库Job
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<bool> Remove(JobsModel model)
        {
            var date = await BaseDal.Db.Deleteable(model).ExecuteCommandAsync();
            if (date > 0)
            {
                return true;
            }
            return false;
        }


        /// <summary>
        /// 修改调度任务
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<bool> Update(JobsModel model)
        {
            var date = await BaseDal.Db.Updateable(model).ExecuteCommandAsync();
            if (date > 0)
            {
                return true;
            }
            return false;
        }
    }
}
